﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace PagedDataGridView
{
    public partial class PagedDataGridView : UserControl
    {
        [Obsolete]
        //Used only inside VS editor. 
        public PagedDataGridView()
        {
            InitializeComponent();
        }

        public PagedDataGridView(string query, string orderByColumn)
        {
            InitializeComponent();
            SetDefaultPageData();
            _pagedQueryExecutor = new AdoNetPagedQueryExecutor();
            _currentPageData.SortBy = orderByColumn;
            _pagedQueryExecutor.SetPage(_currentPageData);
            _pagedQueryExecutor.SetQuery(query);
        }

        public void ReziseControl(int height, int width)
        {
            Size = new Size(width, height);
            groupBoxPageControl.Width = Convert.ToInt32(width*0.88);
            dataGridView.Size = new Size(Convert.ToInt32(width*0.88), height - 80);
            groupBoxPageControl.Location = new Point(24, dataGridView.Height + 10);
        }

        public void Draw()
        {
            comboBoxPerPage.Text = comboBoxPerPage.Items[0].ToString();
            UpdateView(_pagedQueryExecutor.ReadCurrentPage());
            foreach (DataGridViewColumn col in dataGridView.Columns)
                col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            comboBoxPerPage.SelectedIndexChanged += comboBoxPerPage_SelectedIndexChanged;
        }

        private void UpdateView(DataTable dataTable)
        {
            dataGridView.DataSource = dataTable;
            labelSummary.Text = _pagedQueryExecutor.BuildTotalString();
        }

        private void SetDefaultPageData()
        {
            comboBoxPerPage.Text = comboBoxPerPage.Items[0].ToString();
            _currentPageData = new PageData
                                   {
                                       Page = 0,
                                       PageSize = Int32.Parse(comboBoxPerPage.Text),
                                       Ascending = true
                                   };
        }
        public void ApplyFilters()
        {
            UpdateView(_pagedQueryExecutor.ApplyFilters(_filters));
        }

        private void buttonFirstPage_Click(object sender, EventArgs e)
        {
            UpdateView(_pagedQueryExecutor.ReadFirstPage());
        }

        private void buttonPrevPage_Click(object sender, EventArgs e)
        {
            UpdateView(_pagedQueryExecutor.ReadPrevPage());
        }

        private void buttonNextPage_Click(object sender, EventArgs e)
        {
            UpdateView(_pagedQueryExecutor.ReadNextPage());
        }

        private void buttonLastPage_Click(object sender, EventArgs e)
        {
            UpdateView(_pagedQueryExecutor.ReadLastPage());
        }

        private void comboBoxPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(comboBoxPerPage.Text))
                UpdateView(_pagedQueryExecutor.ChangePageSize(Int32.Parse(comboBoxPerPage.Text)));
        }

        internal void AddFilter(Filter filter)
        {
            if (filter == null)
                throw new ArgumentNullException("filter");
            _filters.Add(filter);
            UpdateView(_pagedQueryExecutor.ApplyFilters(_filters));
        }

        private readonly List<Filter> _filters = new List<Filter>();
    }
}